package com.android.interfata;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.menu.MenuAbout;
import com.android.menu.MenuContact;
import com.android.menu.MenuHelp;
import com.android.menu.MenuLegal;

public class Search extends Activity
{
	private String username, pass, id;
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);		
		setContentView(R.layout.searchx);	// eroare ciudata - nu se poate numi search xml-ul?
	
		/* username si logout din heade - le completez cu datele corespunzatoare */
        TextView user = (TextView) findViewById(R.id.userHeader);
        SharedPreferences logPref = getSharedPreferences("LoginPref", MODE_PRIVATE);
    	String username = logPref.getString("user", "username");
    	user.setText(username);		
    	
    	
    	
    	
    	pass = logPref.getString("pass", "pass");
    	id = logPref.getInt("ID", 0) + "";
    	Log.d("date", username + "  " + pass + " " + id);


    	
    	
    	
        TextView logout = (TextView) findViewById(R.id.logoutHeader);
		/**** delogare ****/
		logout = (TextView) findViewById(R.id.logoutHeader);
		logout.setOnClickListener(new View.OnClickListener() 
		{
            public void onClick(View v) 
            {
            	askOK();
            }
        });
		/**** end delogare ****/
		/*
		ImageView searchIView = (ImageView) findViewById(R.id.searchIV);
		searchIView.setOnClickListener(new View.OnClickListener()
        {
			public void onClick(View v) 
			{
								
				EditText searchText = (EditText) findViewById(R.id.searchText); 
				String ST = searchText.getText().toString();
				ST = replace(ST, " ", "%20");
				System.out.println("Search " + ST);
				
				Intent intent = new Intent(getApplicationContext(), SearchList.class);
				intent.putExtra("searchText", ST);
				startActivity(intent);
			}
        });
		*/
		
		Button search = (Button) findViewById(R.id.searchBtn);
		search.setOnClickListener(new View.OnClickListener()
        {
			public void onClick(View v) 
			{
								
				EditText searchText = (EditText) findViewById(R.id.searchText); 
				String ST = searchText.getText().toString();
				ST = replace(ST, " ", "%20");
				System.out.println("Search " + ST);
				
				Intent intent = new Intent(getApplicationContext(), SearchList.class);
				intent.putExtra("searchText", ST);
				intent.putExtra("page", 0);
				startActivity(intent);
			}
        });
		
		
		ImageView scanIV = (ImageView) findViewById(R.id.scanIV);
		scanIV.setOnClickListener(new View.OnClickListener()
        {
			
			public void onClick(View v) 
			{
				Intent intent = new Intent(getApplicationContext(), Scan.class);
				startActivity(intent);
			}
        });
		/*
		Button scan = (Button) findViewById(R.id.scanBtn);
		scan.setOnClickListener(new View.OnClickListener()
        {
			
			public void onClick(View v) 
			{
				Intent intent = new Intent(getApplicationContext(), Scan.class);
				startActivity(intent);
			}
        });*/
	}
	
	/**** delogare ****/
	private void askOK()
	{
		DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
		    public void onClick(DialogInterface dialog, int which)
		    {
		        switch (which)
		        {
			        case DialogInterface.BUTTON_POSITIVE:
			        	delPref();
			        	gotoLogin();
			        	break;
	
			        case DialogInterface.BUTTON_NEGATIVE:
			            //No button clicked
			            break;
		        }
		    }
		};

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Are you sure you want to logout?").setPositiveButton("Yes", dialogClickListener)
		    .setNegativeButton("No", dialogClickListener).show();
	}
	

	private void delPref ()
	{
    	// au nevoie de contextul aplicatiei deci nu le pot pune in clasa pentru delogare
    	SharedPreferences logPref = getSharedPreferences("LoginPref", MODE_PRIVATE);
    	SharedPreferences.Editor prefEditor = logPref.edit();
    	prefEditor.clear();
    	prefEditor.commit();
	}
	
	private void gotoLogin()
	{
		Intent intent = new Intent(getApplicationContext(), Login.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(intent);
	
		finish();
	}
	/**** end delogare ****/
	
	
	static String replace(String str, String pattern, String replace)
	{
		String str1;
		int i = 0;
		while (i < str.length())
		{
			
			if ( str.charAt(i) == ' '  && str.charAt(i+1) == ' ')
			{
				str1 = str.substring(0, i) + str.substring(i + 2, str.length());
				str = str1;
			}
			else
				i++;
		}	
		
	    int s = 0;
	    int e = 0;
	    StringBuffer result = new StringBuffer();

	    while ((e = str.indexOf(pattern, s)) >= 0)
	    {
	        result.append(str.substring(s, e));
	        result.append(replace);
	        s = e+pattern.length();
	    }
	    result.append(str.substring(s));
	    return result.toString();
	}
	
	/*     menu      */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
    	Intent intent;
        switch (item.getItemId()) 
        {
            case R.id.menu_about:     
            	intent = new Intent(getApplicationContext(), MenuAbout.class);
				startActivity(intent);
                break;
            case R.id.menu_help:     
            	intent = new Intent(getApplicationContext(), MenuHelp.class);
				startActivity(intent);
                break;
            case R.id.menu_legal: 
            	intent = new Intent(getApplicationContext(), MenuLegal.class);
				startActivity(intent);
            	break;
            case R.id.menu_contact:
            	intent = new Intent(getApplicationContext(), MenuContact.class);
				startActivity(intent);
            	break;
        }
        return true;
    }
	/*     end menu      */
}
